from game import zz_game
from zz import zz_log, zz_img

log = zz_log.of()


# 寻道大千

class Game(zz_game.ZZ_Game):
    def __init__(self, d, has_prize=True):
        super().__init__(d, has_prize)

    def get_name(self):
        return "无名之辈"

    def game_alert(self):
        self.alert_close_by_file('./game/img/无名之辈')

    def play_alert(self):
        if not self.img_exit("./game/img/无名之辈/play_alert.png", 1):
            self.log_msg("不存在弹窗")
            return False

        if self.img_exit("./game/img/无名之辈/play_state.png", 3):
            self.log_msg("战力下降，点击出售")
            self.img_click("./game/img/无名之辈/play_buy.png")
        else:
            self.log_msg("战力上升，点击收服")
            self.img_click("./game/img/无名之辈/play_get.png")

        if self.img_exit("./game/img/无名之辈/play_alert01.png", 1):
            self.log_msg("存在弹窗确认，点击确认")
            self.img_click("./game/img/无名之辈/play_confirm.png", 2)
            self.img_click("./game/img/无名之辈/play_confirm0.png", 2)
        return True

    def play_boss(self):
        self.log_msg("点击历练")
        self.img_click("./game/img/无名之辈/boss.png")
        if not self.img_exit("./game/img/无名之辈/boss01.png"):
            self.log_msg("等级太低，无法挑战历练")
            return

        # self.img_click_if_exists("点击历练", "./game/img/无名之辈/boss.png", "./game/img/无名之辈/boss01.png")
        self.log_msg("点击武林风云")
        self.img_click("./game/img/无名之辈/boss01.png")
        # self.img_click_if_exists("点击武林风云", "./game/img/无名之辈/boss01.png", "./game/img/无名之辈/boss02.png")
        self.log_msg("点击扫荡")
        self.img_click("./game/img/无名之辈/boss02.png")

        if self.img_exit("./game/img/无名之辈/boss03.png"):
            self.log_msg("点击免费扫荡")
            self.img_click("./game/img/无名之辈/boss03.png")
            self.click_bottom()
        self.click_bottom()
        self.log_msg("点击返回")
        self.img_click("./game/img/无名之辈/boss_exit.png")
        self.log_msg("点击返回")
        self.img_click("./game/img/无名之辈/boss_exit01.png")
        self.log_msg("开始历练-完成")

    def handle_main(self):
        self.log_msg("等待进入登陆页面")
        self.img_exit("./game/img/无名之辈/main.png", 100)
        self.log_msg("已进入登陆页面")
        self.game_alert()
        self.log_msg("点击开始游戏")
        self.img_click("./game/img/无名之辈/start.png")
        self.log_msg("等待进入游戏主界面")
        self.img_exit("./game/img/无名之辈/game_main.png", 100)
        self.log_msg("已进入游戏主界面")

        self.game_alert()

        self.play_boss()

        self.d.sleep(1)
        self.log_msg("判断钓鱼位置")
        play_path = "./game/img/无名之辈/play.png"
        if not self.img_exit("./game/img/无名之辈/play.png", 3):
            play_path = "./game/img/无名之辈/play01.png"

        self.log_msg("开始钓鱼")
        while not self.check_done():
            for i in range(2):
                self.log_msg(f"{i + 1}-点击击钩鱼")
                self.img_click(play_path, 3, 2)
                while self.play_alert():
                    pass

        return True
